package com.neuedu.helloworld.conver;

import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class String2DateConvert implements Converter<String, Date> {
    static List<SimpleDateFormat> list = new ArrayList();

    static{
        list.add(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        list.add(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"));
        list.add(new SimpleDateFormat("yyyy-MM-dd"));
        list.add(new SimpleDateFormat("yyyy/MM/dd"));
    }



    @Override
    public Date convert(String source) {
        for (SimpleDateFormat sdf : list) {

            Date date = null;

            try {
                date = sdf.parse(source);
                return date;
            } catch (ParseException e) {
                //e.printStackTrace();
            }


        }
        return null;
    }
}
